__author__ = 'Soul'

from core.PageFetcher import *
from dboperater.DbOperatorFactory import *
import string
import time

class BaiduAppFetcher(PageFetcher):
    def init(self, fetch_url, recurse=False):
        factory = DbOperatorFactory()
        self.__db__ = factory.GetDbOperator('appdb')
        return PageFetcher.init(self, fetch_url, True)

    def analysis(self):
        if self.__html_src__:
            global seen_urls, urls_to_visit
            from lxml.html import fromstring
            parsed_html = fromstring(self.__html_src__)
            app_info_li_list = parsed_html.xpath("//div[@class='middle']//div[@class='cate_list']/ul/li")
            for app_info in app_info_li_list:
                app_name_div = app_info.xpath("a/div[@class='right']/div[@class='tit_money gray']/h4")
                if type(app_name_div) == list:
                    app_name_div = app_name_div[0]

                app_name = app_name_div.text

                app_star = ''
                app_pbl_ent = ''
                app_desc = ''

                app_size_panel = app_info.xpath("a/div[@class='right']/div[@class='ban_date gray']")[0]
                app_down_dt_panel = app_info.xpath("a/div[@class='right']/div[@class='ban_date gray']")[1]

                app_size = app_size_panel.xpath("span")[0].text
                app_pbl_date = app_down_dt_panel.xpath("span[@class='ban_left']")[0].text
                app_down_count = app_down_dt_panel.xpath("span[@class='date']")[0].text

                app_record = {
                    'app_name': app_name,
                    'app_desc': app_desc,
                    'app_star': app_star,
                    'app_src': '',
                    'app_size': app_size,
                    'app_pbl_ent': app_pbl_ent,
                    'app_pbl_date': app_pbl_date,
                    'app_down_count': app_down_count,
                    'app_fetch_dt': (int)(time.time())
                }

                self.__db__.insert_record(app_record)

            pages_list_panel = parsed_html.xpath("//div[@class='s-index-page filter-app-list-page']")
            if pages_list_panel and len(pages_list_panel):

                if type(pages_list_panel) == list:
                    pages_list_panel = pages_list_panel[0]

                pages_a_list = pages_list_panel.xpath("a")
                pages_a_link_size = len(pages_a_list)
                final_page_div = pages_a_list[pages_a_link_size - 1]
                if final_page_div.attrib['class'] == "next":
                    final_page_div = pages_a_list[pages_a_link_size - 2]

                last_page = final_page_div.text
                last_page = string.atoi(last_page)

                # get the next page URL
                url_str = self.__url__
                items = url_str.split('&')

                pn = string.atoi((items[3]).split('=')[1])
                if pn < last_page * 10:
                    next_pn = pn + 10
                    items[3] = "pn=%d" % (next_pn)
                    next_url = "&".join(items[:4])
                    urls_to_visit.append(next_url)